#include "ImageBitConver.h"
#include <cstdlib>

unsigned char* Image8BitConver::HandleImage(const unsigned char* raw,long size)
{
    unsigned char* newImg=(unsigned char*)malloc(size*3);
    RGB* item=(RGB*)&newImg[0];
    for(int i=0;i<size;i++)
    {
        item->R=raw[i];
        item->G=raw[i];
        item->B=raw[i];
        item++;
    }
    return newImg;
}

//这里填插值算法，输入为Bayer数据，输出为RGB三通道的
unsigned char* Image8BitConver::HandleBayerImage(const unsigned char* raw,long width,long height)
{
  unsigned char* newImg=(unsigned char*)malloc(width*height*3);
  RGB* item=(RGB*)&newImg[0];
  for(int row=0;row<height-1;row+=2)
  {
    for(int col=0;col<width-1;col+=2)
    {
      unsigned char green=(raw[col+(row+1)*width]+raw[col+1+row*width])/2;
      unsigned char blue=raw[col+1+(row+1)*width];
      unsigned char red=raw[col+row*width];
      item[col+1+(row+1)*width]=item[col+row*width]={red,green,blue};
      item[col+1+row*width]={red,raw[col+1+row*width],blue};
      item[col+(row+1)*width]={red,raw[col+(row+1)*width],blue};
    }
  }
  return newImg;
}
